package xyz.kail.tools.pdf.core;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;

/**
 * Created by kail on 2017/7/22.
 */
public class Main {

    private static final String FILE_DIR = "/Users/kail/百度云同步盘/BK/《白帽子讲Web安全》/";
    private static final String IN_FILE_NAME = "白帽子讲Web安全.pdf";
    private static final int BEGIN = 36;
    private static final int END = 49;


    public static void main(String[] args) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(FILE_DIR + IN_FILE_NAME);
        int numberOfPages = reader.getNumberOfPages();
        System.out.println(reader.getNumberOfPages());
        System.out.println(reader.getInfo());

        Rectangle pageSize = reader.getPageSize(1);
        System.out.println(pageSize.toString());
        System.out.println(pageSize.getLeft()); // left x
        System.out.println(pageSize.getBottom()); // left y
        System.out.println(pageSize.getRight()); // right x
        System.out.println(pageSize.getTop()); // right y


        Document splitPdfDoc = new Document(pageSize);

        PdfWriter writer = PdfWriter.getInstance(splitPdfDoc, new FileOutputStream(FILE_DIR + "splitPDF1.pdf"));
        splitPdfDoc.open();

        PdfContentByte pdfContent = writer.getDirectContent();
        for (int i = BEGIN; i <= END; i++) {
            splitPdfDoc.newPage();
            PdfImportedPage importedPage = writer.getImportedPage(reader, i);
            importedPage.setBoundingBox(pageSize);
            pdfContent.addTemplate(importedPage, 0, 0);
        }

        splitPdfDoc.close();
        writer.close();
    }


}
